<?php
date_default_timezone_set("Asia/Shanghai");

if(!isset($_REQUEST['video_url'])){
    echo "parameter lack";
    die();
}

$action="register";
if(isset($_REQUEST['action'])){
    $action=$_REQUEST['action'];
}

$queueDir= __DIR__ . '/../../queue';

$video_url = $_REQUEST['video_url'];
if (empty($video_url)) {
    echo "FAIL EMPTY";
    die();
}
$hash = md5($video_url);

switch ($action){
    case "register":
        $file=$queueDir.'/'.$hash.'.video.init';
        $result=file_exists($file);
        if(!empty($result)){
            if(file_get_contents($file)==$video_url){
                echo "OK ".$hash;
                die();
            }
        }
        $result=file_put_contents($file,$video_url);
        if(!!$result){
            echo "OK ".$hash;
            die();
        }

        echo "FAIL when registering";
        die();
    case "cancel":
        $file=$queueDir.'/'.$hash.'.video.init';
        $result=file_exists($file);
        if($result){
            if(unlink($file)){
                echo "OK";
                die();
            }
            echo "FAIL when cancelling";
            die();
        }
        echo "OK";
        die();
    default:
        echo "???";
        die();
}

